﻿using CSharpTestProject.Utils;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace CSharpTestProject
{
    public partial class Record : Form
    {
        private string[] current;
        private string teacherName;
        private _Sql sql;
        public string TeacherName
        {
            set { teacherName = value; }
        }
        public _Sql Sql
        {
            set { sql = value; }
        }
        public Record()
        {
            current = new string[2];
            InitializeComponent();
        }
        private void Record_Load(object sender, EventArgs e)
        {
            textBox1.Enabled = false;
            string sqlStr = $"SELECT DISTINCT kcm FROM students WHERE teachr_name='{teacherName}'";
            DataSet coursesData = sql.SqlFind(sqlStr);
            int courseNum = coursesData.Tables[0].Rows.Count;
            string[] course = new string[courseNum];
            for (int i = 0; i < courseNum; i++)
            {
                course[i] = coursesData.Tables[0].Rows[i][0].ToString();
            }
            comboBox1.DataSource = course;
        }
        // 根据课程名和时间进行查询，规定是否出勤和备注可以修改，其他只读。
        private void button1_Click(object sender, EventArgs e)
        {
            string shaixuanText = textBox1.Text;
            string shaixuan = comboBox3.Text;
            string shaixuanSql = "";
            if (shaixuan != "")
            {
                switch (shaixuan)
                {
                    case "姓名":
                        shaixuanSql = $" AND name='{shaixuanText}'";
                        break;
                    case "学号":
                        shaixuanSql = $" AND xh='{shaixuanText}'";
                        break;
                    case "已出勤":
                        shaixuanSql = $" AND sfqq='是'";
                        break;
                    case "未出勤":
                        shaixuanSql = $" AND sfqq='否'";
                        break;
                }
            }
            string course = comboBox1.Text;
            if (course == "")
            {
                MessageBox.Show("请选择课程！");
                return;
            }
            string query = $"SELECT * FROM dmb WHERE kcm='{course}' AND sj LIKE '{comboBox2.Text}%'" + shaixuanSql;
            
            DataSet dataset = sql.SqlFind(query);
            int studentNum = dataset.Tables[0].Rows.Count;
            if (studentNum == 0)
            {
                MessageBox.Show("未查询到学生！");
                dataGridView1.DataSource = null;
                return;
            }
            Record_[] records = new Record_[studentNum];
            for (int i = 0; i < studentNum; i++)
            {
                records[i] = new Record_(dataset.Tables[0].Rows[i][2].ToString(),
                    dataset.Tables[0].Rows[i][1].ToString(),
                    dataset.Tables[0].Rows[i][3].ToString(),
                    dataset.Tables[0].Rows[i][4].ToString(),
                    dataset.Tables[0].Rows[i][6].ToString()
                    );
            }
            DataSet dataset2 = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("学号", typeof(string)));
            dt.Columns.Add(new DataColumn("姓名", typeof(string)));
            dt.Columns.Add(new DataColumn("时间", typeof(string)));
            dt.Columns.Add(new DataColumn("是否出勤", typeof(string)));
            dt.Columns.Add(new DataColumn("备注", typeof(string)));
            dt.Columns[0].ReadOnly = true;
            dt.Columns[1].ReadOnly = true;
            dt.Columns[2].ReadOnly = true;
            dt.Columns[3].ReadOnly = true;
            dt.Columns[4].ReadOnly = true;
            DataRow row = dt.NewRow();
            for (int i = 0; i < studentNum; i++)
            {
                row["学号"] = records[i].Id;
                row["姓名"] = records[i].Name;
                row["时间"] = records[i].Time.Substring(0,10);
                row["是否出勤"] = records[i].Sfqq;
                row["备注"] = records[i].Bz;
                dt.Rows.Add(row.ItemArray);
            }
            dataset2.Tables.Add(dt);
            //不允许添加行
            dataGridView1.AllowUserToAddRows = false;
            //背景为白色
            dataGridView1.BackgroundColor = Color.White;
            //只允许选中单行
            dataGridView1.MultiSelect = false;
            //整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //平均分配列宽
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            // 展示在DataGridView上
            dataGridView1.DataSource = dataset2.Tables[0];
        }
        // combobox1修改后的事件函数，取出该课程点名的所有时间
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBox1.Text == "")
            {
                comboBox2.DataSource = null;
                return;
            }
            string queryDate = $"SELECT DISTINCT sj FROM dmb WHERE kcm='{comboBox1.Text}'";
            DataSet dataset2 = sql.SqlFind(queryDate);

            if (dataset2.Tables[0].Rows.Count == 0)
            {
                comboBox2.DataSource = null;
                return;
            }
            string[] dateString = new string[dataset2.Tables[0].Rows.Count];
            for (int i = 0; i < dateString.Length; i++)
            {
                dateString[i] = dataset2.Tables[0].Rows[i][0].ToString().Substring(0, 10);
            }
            dateString = Utils.Utils.DelArraySame(dateString);
            Array.Reverse(dateString);
            comboBox2.DataSource = dateString;
        }
        // 给datagridview加编号
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var dgv = (DataGridView)sender;
            if (dgv.RowHeadersVisible)
            {
                Rectangle rectangle = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, e.RowBounds.Width, e.RowBounds.Height);
                rectangle.Inflate(-3, 0);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, rectangle, e.InheritedRowStyle.ForeColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
            }
        }
        // 根据筛选框设置文本框是否可用
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox3.Text == "" || comboBox3.Text == "已出勤" || comboBox3.Text == "未出勤")
                textBox1.Enabled = false;
            else 
                textBox1.Enabled = true;
        }
        // datagridview数据被修改
        private void dataGridView1_DataSourceChanged(object sender, EventArgs e)
        {
                
        }

        private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int row = e.RowIndex;
            DataTable dt = Utils.Utils.GirdViewToDataTable(dataGridView1);
            ModifyRecord modifyRecord = new ModifyRecord();
            modifyRecord.Row = dt.Rows[row];
            modifyRecord.sql = sql;
            modifyRecord.ShowDialog();
            button1_Click(sender, e);
        }
    }
}
